﻿myApp.controller('BuyRentController', function ($scope, $location, authService, $timeout) {
    $scope.$emit('newPageLoaded', {
        'title': 'Buy / Rent | ShiJie Property',
        'description': "Buy / Rent Property. We Work Very Closely With Developers. Over 40 Years Experience. Buy / Rent Your Property With ShiJie Property Today!",
        'keywords': 'buy property, rent property'
    });

    //$scope.$emit('newPageLoaded', { 'title': 'Penang Realty BUY OR RENT WITH US 世界地產 BUY RENT', 'description': 'We have variety of properties for you to choose.' });

    $scope.property = {};
    pageInit();

    $scope.close = function () {
        $scope.warning = null;
        $scope.error = null;
        $scope.info = null;
    }

    function pageInit() {
        $scope.loader = { loading: false };
        $scope.submissionType = "Buy";
        $scope.property.submissionType = "Buy";
        $scope.propertyType = "Property Type";
        $scope.purposeName = "Invest";
        $scope.property.useInvest = "Y";
    }

    

    $scope.updateSubmissionType = function (submissionType, submissionTypeChanged) {
        //alert(submissionTypeChanged);
        $scope.submissionTypeChanged = submissionTypeChanged;
        $scope.property.submissionType = submissionType;
        $scope.submissionType = submissionType;
    }

    $scope.updatePropertyType = function(propertyType, propertyTypeChanged)
    {
        $scope.propertyTypeChanged = propertyTypeChanged;
        $scope.property.propertyType = propertyType;
        $scope.propertyType = propertyType;
    }

    $scope.updatePurpose = function (purpose, purposeChanged) {
        $scope.purposeChanged = purposeChanged;
        $scope.property.useInvest = purpose;
        switch (purpose) {
            case 'Y':
                $scope.purposeName = "Invest";
                break;
            case 'N':
                $scope.purposeName = "Own Use";
                break;
        }
    }

    $scope.clearEntry = function () {
        $scope.property.name = "";
        $scope.property.phone = "";
        $scope.property.locationKeyword = "";
        $scope.property.propertyType = "Residential";
        $scope.property.submissionType = "Buy";
        $scope.property.useInvest = "";
        $scope.property.remark = "";
        $scope.property.price = 0;
        $scope.close();
    }

    $scope.save = function () {
        window.scrollBy(0, -10000);
        if (!validation())
            return;

        url = 'api/BuyRent/save';
        console.log("DATA PROPERTY: " + JSON.stringify($scope.property));
        $http.post(url, $scope.property)
             .success(function (result) {
                 console.log("Data return!");
                 console.log(result);
                 $scope.data.searchData = result;
                 $scope.info = "Your request has been submitted successfully. We thank you for your enquiry.";
                 $scope.clearEntry();
             })
             .error(function (result) {
                 console.log("Error");
                 console.log(result);
                 $scope.error = "Your request cannot be submitted now. Please contact our office directly for us to serve you immediately.";
             }
         )
    }

   

    $scope.close = function () {
        $scope.warning = null;
        $scope.info = null;
        $scope.error = null;
    }

    function validation() {
        $scope.warning = [];

        if (!$scope.property.locationKeyword)
            $scope.warning.push({ msg: "Location must not be empty!" });
        if (!$scope.property.price)
            $scope.warning.push({ msg: "Budget must not be empty!" });
        if (!$scope.property.name)
            $scope.warning.push({ msg: "Name must not be empty!" });
        if (!$scope.property.phone)
            $scope.warning.push({ msg: "Phone Number must not be empty!" });

        if ($scope.warning.length > 0)
            return false;
        else {
            $scope.warning = null;
            return true;
        }
    }


});